<?php
date_default_timezone_get('PRC');
header('content-type:text/html; charset=utf-8');
$filename="msg";
$msgs=[];
if(file_exists($filename)){
	$string=file_get_contents($filename);
	if(strlen($string) > 0){
		$msgs=unserialize($string);
	}
}

if(isset($_POST['pubMsg'])){
	$username=$_POST['username'];
	$title=strip_tags($_POST['title']);
	$content=strip_tags($_POST['content']);
	$time=time();
	$data=compact('username', 'title', 'content', 'time');
	var_dump($data);
	array_push($msgs, $data);
	$msgs=serialize($msgs);
}

?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>留言板</h1>

	<?php if(is_array($msgs) && count($msgs) > 0): ?>
	<table>
		<thead>
			<tr>
			<th>编号</th>
			<th>昵称</th>
			<th>标题</th>
			<th>时间</th>			
			<th>内容</th>
			</tr>
		</thead>
		<tbody>
			<?php $i=1; foreach($msgs as $val): ?>	
			<tr>
				<td><?php echo $i++; ?></td>
				<td><?php echo $val['username']; ?></td>
				<td><?php echo $val['title']; ?></td>
				<td><?php echo date("m/d/Y H:i:s", $val['time']); ?></td>
				<td><?php echo $val['content']; ?></td>				
			</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
	<?php endif; ?>

	<form action="#" method="post">
		<legend>请留言</legend>
		<label>昵称</label><input type="text" name="username" required/><br />
		<label>标题</label><input type="text" name="title" required/><br />
		<label style="float: left">内容</label><textarea name="content" rows="5" required/></textarea>
		<br />
		<input type="submit" name="pubMsg" value="发布">
	</form>
</body>
</html>

